package com.intellagric.controller.sso.module;

import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.CookieUtils;
import com.intellagric.service.sso.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: zhy
 * @Date: 2018/10/6
 * @Description: 用户登录控制
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Value("${TOKEN_KEY}")
    private String TOKEN_KEY;

    @RequestMapping(value="/user/login", method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage login(String username, String password,
                                 HttpServletRequest request, HttpServletResponse response) {
        ResponseMessage result = loginService.login(username,password);
        if (result.getCode() == 200) {  //登录成功
            String token = (String) result.getData().get("token");
            //添加到cookie中
            CookieUtils.setCookie(request,response,TOKEN_KEY,token);
        }
        return result;
    }

}
